//
//  MyPatientListViewController.m
//  StraightTrain
//
//  Created by mk on 15/11/27.
//  Copyright © 2015年 mk. All rights reserved.
//

#import "MyPatientListViewController.h"
#import "PatienUserData.h"
#import "MyPatientListCell.h"
#import "PatientDetailsViewController.h"
#import "CreatePatientMainViewController.h"
#import "BookingDetailsViewController.h"

@interface MyPatientListViewController ()<MyPatientListCellDelegate>
{
    NSInteger selectTable;
    PatienUserListData * patienHasList;//已经预约列表
    PatienUserListData * patienNOList;//等待预约列表
}

@end

@implementation MyPatientListViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
  
    [self.navigationItem setTitleView:segment];
    segment.backgroundColor = defaultNavBackColor;
    segment.normalColor = [UIColor whiteColor];
    segment.highlightedColor = defaultNavBackColor;
    [segment updateTextColor];
    
    btnAddPatient.layer.contentsScale = 0;
    btnAddPatient.layer.borderWidth = 2;
    btnAddPatient.layer.borderColor = defaultNavBackColor.CGColor;
    
    tabNoBooking.refreshDelegate = self;
    tabHasBooking.refreshDelegate = self;
 
    selectTable = 0;
    
    patienNOList = [[PatienUserListData alloc]init];
    patienHasList = [[PatienUserListData alloc]init];
    [tabNoBooking becomeRefreshing:YES];
    self.view.backgroundColor = UIColorFromRGB(0xff, 0xff, 0xff);
}


#pragma mark 添加患者
-(IBAction)btnAddPatient:(id)sender
{
    CreatePatientMainViewController * create = [[CreatePatientMainViewController alloc]initWithNibName:@"CreatePatientMainViewController" bundle:nil];
    [self.navigationController pushViewController:create animated:YES];

}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   // DLog(@"==%@",patienNOList.dataArray);
    if(tableView == tabHasBooking)
    return patienHasList.dataArray.count;
    else
        return patienNOList.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 110;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellTableIdentifier = @"MyPatientListCellStr";
    
    MyPatientListCell *cell = (MyPatientListCell *)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
    if (!cell)
    {
        cell = (MyPatientListCell *)[[[NSBundle mainBundle] loadNibNamed:@"MyPatientListCell" owner:self options:nil] objectAtIndex:0];
    }
    if (tableView == tabHasBooking)
    [cell fillDateWith:[patienHasList.dataArray objectAtIndex:indexPath.row] isDelete:NO];
    else
     [cell fillDateWith:[patienNOList.dataArray objectAtIndex:indexPath.row] isDelete:YES];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PatienUserInfoData * info= [[PatienUserInfoData alloc] init];
    if (tableView == tabNoBooking)//待预约
    {
        info = [patienNOList.dataArray objectAtIndex:indexPath.row];
        PatientDetailsViewController * detail = [[PatientDetailsViewController alloc]initWithNibName:@"PatientDetailsViewController" bundle:nil];
        detail.strID = info.patien_id;
        detail.strTitle = @"患者详情";
        [self.navigationController pushViewController:detail animated:YES];
    }
    else
    {
       info = [patienHasList.dataArray objectAtIndex:indexPath.row];
        PatientDetailsViewController * detail = [[PatientDetailsViewController alloc]initWithNibName:@"PatientDetailsViewController" bundle:nil];
        detail.strID = info.patien_id;
        detail.strTitle = @"预约详情";
        [self.navigationController pushViewController:detail animated:YES];

    }
    
}


- (void)refreshTableViewDidTriggerRefresh:(RefreshTableView*)refreshTableView
{

    NSString * strUrl= [NSString stringWithFormat:@"%@?username=%@&token=%@",[gProjConfig.dicAllUrl objectForKey:@"myPatientUrl"],[gProjConfig.dicUserData objectForKey:USER_tel],[gProjConfig.dicUserData objectForKey:USER_token]];
    
    [[HttpClient manager]GET:strUrl
                  parameters:nil
                     success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         id dateBooking = [[responseObject objectForKey:requestDataKey] objectForKey:@"hasBookingList"];
         if ([dateBooking isKindOfClass:[NSArray class]])
         {
            patienHasList.dataArray = [[NSMutableArray alloc]init];
            
            for (int i = 0; i < [dateBooking count]; i++)
             {
                 NSDictionary *dicRec = [dateBooking objectAtIndex:i];
                 NSDictionary * dicpatientInfo = [dicRec objectForKey:@"patientInfo"];
                 PatienUserInfoData *recData = [[PatienUserInfoData alloc] init];
                 [recData parseData:dicpatientInfo];
                 [patienHasList.dataArray addObject:recData];
             }
           
             if (patienHasList.dataArray.count >0)
             {
                 viewHasNoData.hidden = YES;
                 self.view.backgroundColor = UIColorFromRGB(0xf0,0xf0,0xf0);
                 [tabHasBooking reloadData];
             }
             else
             {
                 if(!tabHasBooking.hidden)
                 {
                     viewHasNoData.hidden = NO;
                     self.view.backgroundColor = [UIColor whiteColor];
                 }
             }
             
             int count = (int)[dateBooking count];
             NSString * strTitle = (count>100?@"已预约(99+)":[NSString stringWithFormat:@"已预约(%d)",count]);
             [btnSegment2 setTitle:strTitle forState:UIControlStateNormal];
             tabHasBooking.backgroundColor = self.view.backgroundColor;
         }
    
         dateBooking = [[responseObject objectForKey:requestDataKey] objectForKey:@"noBookingList"];
         if ([dateBooking isKindOfClass:[NSArray class]])
         {
             patienNOList.dataArray = [[NSMutableArray alloc]init];
            
            for (int i = 0; i < [dateBooking count]; i++)
            {
                 NSDictionary *dicRec = [dateBooking objectAtIndex:i];
                 NSDictionary * dicpatientInfo = [dicRec objectForKey:@"patientInfo"];
                 PatienUserInfoData *recData = [[PatienUserInfoData alloc] init];
                 [recData parseData:dicpatientInfo];
                 [patienNOList.dataArray addObject:recData];
            }
            if (patienNOList.dataArray.count >0)
             {
                 viewNoData.hidden = YES;
                 self.view.backgroundColor = UIColorFromRGB(0xf0,0xf0,0xf0);
                 [tabNoBooking reloadData];
             }
             else
             {
                 if(!tabNoBooking.hidden)
                 {
                   viewNoData.hidden = NO;
                   self.view.backgroundColor = [UIColor whiteColor];
                 }
             }
             int count = (int)[dateBooking count];
             NSString * strTitle = (count>100?@"待预约(99+)":[NSString stringWithFormat:@"待预约(%d)",count]);
             [btnSegment1 setTitle:strTitle forState:UIControlStateNormal];
             tabNoBooking.backgroundColor = self.view.backgroundColor;
         }

         [refreshTableView refreshFinished];
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         [refreshTableView refreshFinished];

         HUDShowRequestError(error.domain, self.view)
     }];
}



#pragma -mark delegates

-(void)customSegDidSelected:(CustomSegmentView *)segmentView
{
    if (segmentView.selectedIndex == 1)//
    {
        tabNoBooking.hidden = NO;
        tabHasBooking.hidden = YES;
        if (!viewHasNoData.hidden)
        {
            viewHasNoData.hidden = YES;
        }
        if (patienNOList.dataArray.count==0)
            [tabNoBooking becomeRefreshing:NO];
        
    }else if(segmentView.selectedIndex == 2)//
    {
        tabNoBooking.hidden = YES;
        tabHasBooking.hidden = NO;
        if (!viewNoData.hidden)
        {
            viewNoData.hidden = YES;
        }
        if (patienHasList.dataArray.count==0)
            [tabHasBooking becomeRefreshing:NO];
    }
}


-(void)delegatePatientWith:(MyPatientListCell *)cell
{

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
